import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:openim_common/openim_common.dart';

import 'edit_announcement_logic.dart';

class EditGroupAnnouncementPage extends StatelessWidget {
  final logic = Get.find<EditGroupAnnouncementLogic>();

  EditGroupAnnouncementPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TitleBar.back(
        title: logic.title,
        right: Obx(() => GestureDetector(
          onTap: logic.canSave ? () {
            print('保存按钮被点击，canSave: ${logic.canSave}');
            logic.save();
          } : null,
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
            child: StrRes.save.toText
              ..style = logic.canSave 
                  ? Styles.ts_0C1C33_17sp 
                  : Styles.ts_8E9AB0_17sp,
          ),
        )),
      ),
      backgroundColor: Styles.c_FFFFFF,
      body: SafeArea(
        child: SingleChildScrollView(
          child: ConstrainedBox(
            constraints: BoxConstraints(
              minHeight: MediaQuery.of(context).size.height - 
                         MediaQuery.of(context).padding.top - 
                         kToolbarHeight - 
                         MediaQuery.of(context).padding.bottom,
            ),
            child: IntrinsicHeight(
              child: Column(
                children: [
                  // 提示信息
                  Container(
                    width: double.infinity,
                    padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
                    margin: EdgeInsets.all(10.w),
                    decoration: BoxDecoration(
                      color: Styles.c_F8F9FA,
                      borderRadius: BorderRadius.circular(8.r),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        StrRes.groupAcPermissionTips.toText
                          ..style = Styles.ts_8E9AB0_14sp,
                        4.verticalSpace,
                        StrRes.plsEnterGroupAc.toText
                          ..style = Styles.ts_8E9AB0_12sp,
                      ],
                    ),
                  ),
                  
                  // 输入框
                  Expanded(
                    child: Container(
                      margin: EdgeInsets.symmetric(horizontal: 10.w),
                      padding: EdgeInsets.all(12.w),
                      decoration: BoxDecoration(
                        color: Styles.c_F8F9FA,
                        borderRadius: BorderRadius.circular(8.r),
                      ),
                      child: Column(
                        children: [
                          Expanded(
                            child: TextField(
                              controller: logic.inputCtrl,
                              style: Styles.ts_0C1C33_14sp,
                              maxLines: null,
                              expands: true,
                              textAlignVertical: TextAlignVertical.top,
                              inputFormatters: [
                                LengthLimitingTextInputFormatter(logic.maxLength)
                              ],
                              decoration: InputDecoration(
                                border: InputBorder.none,
                                hintText: logic.isReadOnly.value ? '' : '请输入群公告内容...',
                                hintStyle: Styles.ts_8E9AB0_16sp,
                              ),
                            ),
                          ),
                          
                          // 字符计数和操作按钮
                          Padding(
                            padding: EdgeInsets.only(top: 8.h),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Flexible(
                                  child: Row(
                                    mainAxisSize: MainAxisSize.min,
                                    children: [
                                      GestureDetector(
                                        onTap: logic.clearAnnouncement,
                                        child: Container(
                                          padding: EdgeInsets.symmetric(
                                            horizontal: 12.w, 
                                            vertical: 6.h
                                          ),
                                          decoration: BoxDecoration(
                                            color: Styles.c_E8EAEF,
                                            borderRadius: BorderRadius.circular(4.r),
                                          ),
                                          child: Text(
                                            '清空',
                                            style: TextStyle(
                                              color: Styles.c_FF381F,
                                              fontSize: 12.sp,
                                            ),
                                          ),
                                        ),
                                      ),
                                      8.horizontalSpace,
                                      GestureDetector(
                                        onTap: logic.resetAnnouncement,
                                        child: Container(
                                          padding: EdgeInsets.symmetric(
                                            horizontal: 12.w, 
                                            vertical: 6.h
                                          ),
                                          decoration: BoxDecoration(
                                            color: Styles.c_E8EAEF,
                                            borderRadius: BorderRadius.circular(4.r),
                                          ),
                                          child: Text(
                                            '重置',
                                            style: TextStyle(
                                              color: Styles.c_0089FF,
                                              fontSize: 12.sp,
                                            ),
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                Obx(() => '${logic.currentLength}/${logic.maxLength}'.toText
                                  ..style = logic.currentLength > logic.maxLength 
                                      ? Styles.ts_FF381F_12sp 
                                      : Styles.ts_8E9AB0_12sp),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  
                  SizedBox(height: 20.h),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}